package com.cea.papyrus.diagram.activity.editpart;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.uml2.uml.Action;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.OpaqueAction;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.figures.IconFigure;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagram.activity.figure.ActionFigure;
import com.cea.papyrus.diagram.activity.figure.OpaqueActionBodyFigure;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.umlutils.ICustomAppearence;

public class OpaqueActionEditPart extends ActionEditPart {

	public OpaqueActionEditPart(GraphNode graphNode, ModelManager modelManager) {
		super(graphNode, modelManager);
	}

	public OpaqueAction getUmlElement() {
		return (OpaqueAction) super.getUmlElement();
	}
	
	protected IFigure createFigure() {
		// Force initialization of the manager when figure has changed
		// manager = null;		
		// Try to get figure from GraphNodeWithSemanticEditPart
		IFigure fig = super.createFigure();
		if (fig instanceof ActionFigure) {
			return new OpaqueActionBodyFigure();
		}
		return fig;
	}

	protected Label getNameLabel() {
		IFigure fig = getFigure();

		if (fig instanceof IconFigure) {
			return (Label) fig;
		} else if (fig instanceof ActionFigure) {
			return ((ActionFigure)fig).getNameLabel();
		} else if (fig instanceof OpaqueActionBodyFigure) {
			return ((OpaqueActionBodyFigure)fig).getNameLabel();
		}
		else {
//			Debug.error(this, "ActionEditPart::getNameLabel() : Unknown Figure type.", channel);
			return null;
		}
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.editpart.GraphNodeWithSemanticEditPart#refreshVisuals()
	 */
	@Override
	protected void refreshVisuals() {

		Dimension size = getGraphNode().getSize();
		Point p = getGraphNode().getPosition();
		Rectangle r = new Rectangle(p, size);
		int style = 0;
		
		IFigure fig = getFigure();

		// Classifier
		if (fig instanceof IconFigure) {
			IconFigure icf = (IconFigure) getFigure();
			icf.setName(((NamedElement) getUmlElement()).getName());
		} else {
			Action currentAction = getUmlElement();
			com.cea.papyrus.umlutils.Element currentElement = new com.cea.papyrus.umlutils.Element(getUmlElement());
			((OpaqueActionBodyFigure) fig).refreshColors(getGraphNode());
			((OpaqueActionBodyFigure) fig).refreshPresentation(getPresentation(),currentAction, getGraphNode());
			
			boolean hasIcon = currentElement.hasIcon();
			boolean hasShape = currentElement.hasShape();
		
//			((OpaqueActionBodyFigure) fig).refreshStereotypes(((ActionFigure) fig).stereotypesLabel(this), getPresentation(), hasIcon, hasShape);
			((OpaqueActionBodyFigure) fig).setName(" ");
			((OpaqueActionBodyFigure) fig).setBorderColor(ColorConstants.black);
			
			String value = getProperty(ICustomAppearence.CUSTOM_APPEARENCE_KEY);
			if(value == null) {
				((OpaqueActionBodyFigure) fig).setName(currentAction.getName());
			}
			else {
				try {style = Integer.parseInt(value);} 
				catch (NumberFormatException e) {
					Debug.log(e);
				}
				if ((style & ICustomAppearence.DISP_NAME) != 0) {
					((OpaqueActionBodyFigure) fig).setName(currentAction.getName());
				}
				else {
					((OpaqueActionBodyFigure) fig).setName("");
				}
				if ((style & ICustomAppearence.DISP_BODY) != 0) {
					((OpaqueActionBodyFigure) fig).setBody(getUmlElement().getBodies().get(0));
				}
				else {
					((OpaqueActionBodyFigure) fig).setBody("");
				}
			}

			// qualifiedName
			GraphNode gp= (GraphNode) getGraphNode().getContainer();
			((OpaqueActionBodyFigure) fig).refreshQualifiedName(gp, getGraphNode(), getUmlElement());
		}  

		((GraphicalEditPart) getParent()).setLayoutConstraint(this, fig, r);
	}
	@Override
	public IFigure getContentPane() {
		if (getFigure() instanceof OpaqueActionBodyFigure) {
			return ((OpaqueActionBodyFigure) getFigure()).getContentPane();
		} else {
			// Figure is a shape
			return getFigure();
		}
	}
}
